package com.txf.coomon.mvp.net_retrofit.base;

import android.content.Context;

import com.txf.coomon.net.net_retrofit.listener.AppRequestListener;
import com.txf.coomon.net.net_retrofit.ProgressObserver;
import com.txf.ui_mvplibrary.mvp.model.BaseModel;

import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author txf
 * @create 2019/1/29 0029
 * @
 */

public class BaseAppModel<T> extends BaseModel {
    /**
     * 封装线程管理和订阅的过程
     * flag  是否添加progressdialog
     */
    protected void subscribe(Context context, final Observable observable, AppRequestListener<T> listener, ObservableTransformer<T, T> transformer,
                             boolean isDialog, boolean cancelable) {

        final Observer<T> observer = new ProgressObserver(context,listener, isDialog, cancelable);
        observable.compose(transformer)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    protected void subscribe(Context context,final Observable observable, AppRequestListener<T> listener, ObservableTransformer<T, T> transformer) {
        subscribe(context, observable, listener, transformer, true, false);
    }

}
